package com.bw.mvvm.custom;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.bw.mvvm.R;

public class BottomCustom extends RelativeLayout implements View.OnClickListener {

    private LinearLayout linearLayout;
    private ImageView imageView;
    private TextView textView;
    private BottomSelectLinstenner linstenner;
    private boolean selFlag;
    private int nomalSize = SizeUtils.sp2px(5);
    private int selSize = SizeUtils.sp2px(7);
    private int nomalColor = Color.BLACK;
    private int selColor = Color.RED;
    private int nomalImag = 0;
    private int selImag = 0;
    private String content = "";

    public BottomCustom setLinstenner(BottomSelectLinstenner linstenner) {
        this.linstenner = linstenner;
        return this;
    }
    public BottomCustom setConten(String content){
        this.content = content;
        return this;
    }

    public BottomCustom setNomalSize(int nomalSize){
        this.nomalSize = ConvertUtils.sp2px(nomalSize);
        return this;
    }

    public BottomCustom setSelSize(int slelSize) {
        this.selSize = ConvertUtils.sp2px(slelSize);
        return this;
    }

    public BottomCustom setSelImag(int selImag) {
        this.selImag = selImag;
        return this;
    }
    public BottomCustom setNomalImag(int nomalImag) {
        this.nomalImag = nomalImag;
        return this;
    }
    public BottomCustom setNomalColor(int nomalColor) {
        this.nomalColor = nomalColor;
        return this;
    }

    public BottomCustom setSelColor(int selColor) {
        this.selColor = selColor;
        return this;
    }

    public BottomCustom setSelFlag(boolean flag) {
        this.selFlag = flag;
        refresh();
        return this;
    }

    public BottomCustom(Context context) {
        super(context);
    }

    public BottomCustom(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.bottom_arrs);
        selFlag = typedArray.getBoolean(R.styleable.bottom_arrs_selflag, false);
        typedArray.recycle();
        init();
    }

    private void init() {
        linearLayout = new LinearLayout(getContext());
        RelativeLayout.LayoutParams layoutParams = new LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.addRule(CENTER_IN_PARENT);
        linearLayout.setLayoutParams(layoutParams);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setGravity(Gravity.CENTER_HORIZONTAL);
        imageView = new ImageView(getContext());
        LinearLayout.LayoutParams imgParams = new LinearLayout.LayoutParams(
                ConvertUtils.dp2px(25),ConvertUtils.dp2px(30));
        imageView.setLayoutParams(imgParams);
        linearLayout.addView(imageView);
        textView = new TextView(getContext());
        LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        textView.setLayoutParams(tvParams);
        linearLayout.addView(textView);
        addView(linearLayout);
        setOnClickListener(this);
    }

    public BottomCustom(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void onClick(View view) {
        selFlag = false;
        refresh();
        if (linstenner != null){
            linstenner.onselect(view.getId());
        }
    }
    public interface BottomSelectLinstenner{
        void onselect(int id);
    }

    public void refresh(){
        int imagres = nomalImag;
        int tvColor = nomalColor;
        int tvSize = nomalSize;
        if (selFlag){
            imagres = selImag;
            tvColor = selColor;
            tvSize = selSize;
        }
        imageView.setImageResource(imagres);
        textView.setTextColor(tvColor);
        textView.setTextSize(tvSize);
        textView.setText(content);
    }
}
